package com.jinyu.jyly.ui.mine.setting.login_psw;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 交易密码
 */
public class TranPswActivity extends BaseActivity<LoginPswPresenter> implements LoginPswContract.View {


    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.tv_code)
    EditText tvCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_new_psw)
    EditText etNewPsw;
    @BindView(R.id.et_again_psw)
    EditText etAgainPsw;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.et_old_psw)
    EditText et_old_psw;
    private String mobile;
    private LoginPswPresenter presenter;
    private int mSmsId;

    @Override
    public int getLayout() {
        return R.layout.activity_change_tran_psw;
    }

    @Override
    protected LoginPswPresenter getPresenter() {
        if (presenter == null) {
            presenter = new LoginPswPresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        mobile = getIntent().getStringExtra("mobile");
    }


    @OnClick({R.id.iv_back, R.id.tv_get_code, R.id.btn_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_get_code:
                boolean showing = mTipDialog.isShowing();
                if (!showing) {
                    mTipDialog.show();
                    presenter.getSmsCode();
                }


//                try {
//                    JSONObject jsonObject = new JSONObject();
//                    jsonObject.put("mobile", mobile);
//                    jsonObject.put("template", "findpassword");
//                    String pamam = AES.encryptToHexString(jsonObject.toString());

//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
                break;
            case R.id.btn_sub:

//                String old_psw = et_old_psw.getText().toString().trim();
                String Code = tvCode.getText().toString().trim();
                String new_psw = etNewPsw.getText().toString().trim();
                String again_psw = etAgainPsw.getText().toString().trim();

                if (TextUtils.isEmpty(Code)) {
                    showToast("验证码不能为空");
                    return;
                }
//                if (TextUtils.isEmpty(old_psw)) {
//                    showToast("原密码不能为空");
//                    return;
//                }

                if (TextUtils.isEmpty(new_psw)) {
                    showToast("新密码不能为空");
                    return;
                }

                if (!new_psw.equals(again_psw)) {
                    showToast("两次密码输入不一致");
                    return;
                }

                boolean showing1 = mTipDialog.isShowing();
                if (!showing1) {
                    try {
                        mTipDialog.show();
                        JSONObject jsonObject = new JSONObject();
//                    jsonObject.put("old_pwd", old_psw);
                        jsonObject.put("verycode", Code);
                        jsonObject.put("pwd", new_psw);
                        jsonObject.put("smsid", mSmsId);
                        jsonObject.put("cat", "1");
                        String pamam = AES.encryptToHexString(jsonObject.toString());
                        presenter.changePassword(pamam);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
    }

    @Override
    public void onSmsCode(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()) {
            case 200:
                try {
                    String data = infoBean.getData();
                    String result = AES.decrypt(data);
                    JSONObject jsonObject = new JSONObject(result);
                    mSmsId = jsonObject.optInt("id");
                    downTimer.start();
                    tvGetCode.setEnabled(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                tvGetCode.setEnabled(true);
                tvGetCode.setText("获取验证码");
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    public void onChangePassword(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()) {
            case 200:
                String data = infoBean.getData();
                showToast("修改密码成功");
                finish();
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }


    CountDownTimer downTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long arg0) {
            tvGetCode.setText("重新发送(" + arg0 / 1000 + ")");
            tvGetCode.setEnabled(false);
        }

        @Override
        public void onFinish() {
            tvGetCode.setEnabled(true);
            tvGetCode.setText("获取验证码");
        }
    };
}
